package org.example;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import java.util.Arrays;
import java.util.List;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("my-persistence-unit");
        EntityManager em = emf.createEntityManager();
        nativeQuery(em,"SHOW TABLES");
        nativeQuery(em,"SHOW COLUMNS FROM REPORT");
        nativeQuery(em,"SHOW COLUMNS FROM SUB_REPORTS");
        persistReport(em);
        nativeQuery(em,"SELECT * FROM REPORT");
        nativeQuery(em,"SELECT * FROM SUB_REPORTS");
        em.close();
    }
    private static void persistReport(EntityManager em){
        Report report = new Report();
        report.setReportContent("a very big report...");
        report.setReportImage(new byte[]{1,2,3,4,5,6,7,8,9});
        report.setSubReportContents(Arrays.asList("sub report1...","sub report2..."));
        em.getTransaction().begin();
        em.persist(report);
        em.getTransaction().commit();
    }
    private static void nativeQuery(EntityManager em,String s){
        System.err.println("-----------" + s + "----------------");
        Query query = em.createNativeQuery(s);
        List list = query.getResultList();
        for (Object o : list) {
            if (o instanceof Object[]) {
                System.err.println(Arrays.toString((Object[])o));
            }else{
                System.err.println(o);
            }
        }
    }
}
